"""
ASGI config for website project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application
from .websocket import websocket_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings')

http_application = get_asgi_application()

async def application(scope, receive, send):
        type = scope['type']
        if type == 'http':
            await http_application(scope, receive, send)
        elif type == 'websocket':
            await websocket_application(scope, receive, send)
        else:
            raise Exception('[ERR] Unknown scope type : ' , scope['type'])


'''scope的内容：
{
    'type': 'websocket', 
    'path': '/', 
    'raw_path': b'/', 
    'headers': [
        (b'host', b'localhost:8000'), 
        (b'connection', b'Upgrade'), 
        (b'pragma', b'no-cache'), 
        (b'cache-control', b'no-cache'), 
        (b'user-agent', b'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36'), 
        (b'upgrade', b'websocket'), 
        (b'origin', b'http://localhost:8000'), 
        (b'sec-websocket-version', b'13'), 
        (b'accept-encoding', b'gzip, deflate, br'), 
        (b'accept-language', b'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7'), 
        (b'sec-websocket-key', b'/gFeehJUsPMxw9kEZ+cPPg=='), 
        (b'sec-websocket-extensions', b'permessage-deflate; client_max_window_bits')
        ], 
    'query_string': b'', 
    'client': ['127.0.0.1', 54641], 
    'server': ['127.0.0.1', 8000], 
    'subprotocols': [], 
    'asgi': {'version': '3.0'}}
'''